package com.gsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gsc.constants.SystemConstants;
import com.gsc.domain.ResponseResult;
import com.gsc.domain.entity.Article;
import com.gsc.domain.entity.Category;
import com.gsc.domain.vo.CategoryVo;
import com.gsc.domain.vo.PageVo;
import com.gsc.mapper.CategoryMapper;
import com.gsc.service.ArticleService;
import com.gsc.service.CategoryService;
import com.gsc.utils.BeanCopyUtils;
import net.sf.jsqlparser.statement.select.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-01-28 09:58:39
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;

    /**
     * 获取分类列表业务代码
     * @return List<CategoryVo>
     */
    @Override
    public ResponseResult getCategoryList() {

        /*
            分步查询，先查文章表，再查分类表
        */
        //1.查询文章表，状态为已发布的文章
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(queryWrapper);
        //2.获取文章的分类id，去重， 转换成Set集合去重
        Set<Long> categoryIds = articleList.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());

        //3.查询分类表
        List<Category> categories = listByIds(categoryIds);
        //过滤，保留正常状态的分类
        categories.stream()
                .filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());

        //4.封装vo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    /**
     * 查询所有分类接口【后台写博文】
     * @return
     */
    @Override
    public ResponseResult listAllCategory() {

        //查询出正常状态的分类数据
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus,SystemConstants.STATUS_NORMAL);
        List<Category> list = list(queryWrapper);
        //封装成vo返回
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public PageVo categoryPageList(Integer pageNum, Integer pageSize, Category category) {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(category.getStatus()),Category::getStatus,category.getStatus());
        queryWrapper.like(StringUtils.hasText(category.getName()),Category::getName,category.getName());
        //分页查询
        Page page = new Page(pageNum,pageSize);
        page(page,queryWrapper);

        //封装数据返回
        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());
        return pageVo;
    }

}
